package hjw.com.controller.adm;

import hjw.com.entity.Hot;
import hjw.com.entity.LargeFile;
import hjw.com.service.HotServiceImpl;
import hjw.com.service.LargeFileServiceImpl;
import hjw.com.util.PackJsn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * <p>Project:Academic - HotController
 *
 * @author IvanWei玮 -- maste
 * @version 1.0
 * @DataTime 2025/2/19 17:42
 * @since 1.8
 */
@RestController
@RequestMapping("api/adm/hot")
public class AdmHotController {
    private static final Random RANDOM = new Random();
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final LargeFileServiceImpl largeFileDao;
    private final HotServiceImpl hotDao;

    @Autowired
    public AdmHotController(LargeFileServiceImpl largeFileDao, HotServiceImpl hotDao) {
        this.largeFileDao = largeFileDao;
        this.hotDao = hotDao;
    }

    @GetMapping("/findByParam")
    public String findByParam(Integer id,Integer type) {
        Map<String, Object> m = new HashMap<>(2);
        if (id != null){
            m.put("id",id);
        }
        if (type != null){
            m.put("type",type);
        }

        logger.info("HotFindByParam=" + m);
        List<Hot> hots = hotDao.findByParam(m);
        logger.info("hots=" + hots);
        if (hots== null || hots.isEmpty()){
            return PackJsn.pack(203, "库中无轮播图广告图信息");
        }
        return PackJsn.pack(200, "轮播图广告图信息", hots);
    }
    @GetMapping("/del/{id}")
    public String del(@PathVariable("id") Integer id) {
        logger.info("HotDel=" + id);
        int i = hotDao.delete(id);
        if (i == 0){
            return PackJsn.pack(203, "删除失败");
        }
        return PackJsn.pack(200, "删除成功");
    }


    //此处 AdmBookController("/updateAll")代码可观赏性 比 PublicController("/add")的高

    @PostMapping("/add")
    public String updateById(@RequestParam Map<String, Object> map,@RequestParam(value = "mypic", required = false) MultipartFile myfile) {
        logger.info("myfile"+myfile);
        try {
            logger.info("HotAdd=" + map);
            //第一种情况，为修改图片，修改图片必须上传原图片id
            if (myfile != null && !myfile.isEmpty() && map.get("picture")!=null && !map.get("picture").toString().isEmpty()){
                LargeFile largeFile = new LargeFile();
                //获取新图片的uuid信息
                UUID uuid = UUID.randomUUID();
                //截取新图片的id
                String newImageId = uuid.toString().replace("-", "");

                String picture = map.get("picture").toString();
                //修改前先删除原图片
                largeFileDao.delete(picture);
                largeFile.setId(newImageId);
                largeFile.setFilename(myfile.getOriginalFilename());
                largeFile.setContent(myfile.getBytes());
                int add = largeFileDao.add(largeFile);
                if (add == 0){
                    logger.error("修改轮播图广告图失败");
                }
                map.put("picture", newImageId);
            }

            if(Integer.parseInt(map.get("id").toString()) != 0){
                if (map.get("picture")!=null && map.get("picture").toString().isEmpty()){
                    map.remove("picture");
                }
                int i = hotDao.updateById(map);
                if (i == 0){
                    logger.error("修改轮播图广告图失败");
                    return PackJsn.pack(203, "更新失败",map.get("id"));
                }
                return PackJsn.pack(200, "更新成功",map.get("id"));
            }
            if (myfile == null || myfile.isEmpty()){
                return PackJsn.pack(203, "图片不能为空",map.get("id"));
            }
            //第二种情况，为添加图片，并且是新增的轮播广告信息 关键判断：Integer.parseInt(map.get("id").toString()) == 0
            if (myfile != null && !myfile.isEmpty() && map.get("id")!=null && Integer.parseInt(map.get("id").toString()) == 0){
                LargeFile largeFile = new LargeFile();
                //获取新图片的uuid信息
                UUID uuid = UUID.randomUUID();
                //截取新图片的id
                String newImageId = uuid.toString().replace("-", "");
                largeFile.setId(newImageId);
                largeFile.setFilename(myfile.getOriginalFilename());
                largeFile.setContent(myfile.getBytes());
                int imgAdd = largeFileDao.add(largeFile);
                Hot hot = new Hot();
                hot.setMemo(map.get("memo").toString());
                hot.setPicture(newImageId);
                hot.setHot(Integer.parseInt(map.get("hot").toString()));
                hot.setType(Integer.parseInt(map.get("type").toString()));
                int hotAdd = hotDao.add(hot);
                if (imgAdd == 0 || hotAdd == 0){
                    return PackJsn.pack(203, "添加失败",hot.getId());
                }
                return PackJsn.pack(200, "添加成功",hot.getId());
            }

        } catch (IOException e) {
            return PackJsn.pack(203, "图片更新异常");
        }
        return PackJsn.pack(200, "更新成功");
    }
}
